package com.scm.biz.service._impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.scm.biz.bean.BusinessFile;
import com.scm.biz.dao.BusinessFileDao;
import com.scm.biz.service.BusinessFileService;
import com.scm.core.bean.exp.UfdmException;
import com.scm.utils.UploadAndDownUtil;

import net.sf.json.JSONArray;

/**
 * 业务附件业务类实现类
 * @author Young
 * @Date 2016.11.16
 */
@Service("businessFileService")
public class BusinessFileServiceImpl implements BusinessFileService{
	
	@Resource
	private BusinessFileDao businessFileDao;

	@Override
	public void insertList(List<MultipartFile> attachs, Long businessId,
			String fileCategory,int flag) throws UfdmException {
		for(int i =0;i<attachs.size();i++){
			BusinessFile businessFile=new BusinessFile();
			businessFile.setBusinessId(businessId);
			businessFile.setCreatedAt(new Date());
			if(flag == 0){
				businessFile.setBusinessFile(UploadAndDownUtil.uploadFile(attachs.get(i), fileCategory));
			}else if(flag ==1){
				businessFile.setBusinessFile(UploadAndDownUtil.uploadImage(attachs.get(i), fileCategory));
			}
			businessFile.setOriginName(attachs.get(i).getOriginalFilename());
			businessFile.setCategory(fileCategory);
			businessFileDao.insert(businessFile);
		}
		
		
	}

	@Override
	public void insert(MultipartFile multipartFile, Long businessId,
			String fileCategory,Integer flag) throws UfdmException {
		if(multipartFile ==null){
			return;
		}
		BusinessFile businessFile = new BusinessFile();
		businessFile.setBusinessId(businessId);
		businessFile.setCreatedAt(new Date());
		if(flag!=null &&flag == 0){
			businessFile.setBusinessFile(UploadAndDownUtil.uploadFile(multipartFile, fileCategory));
		}else if(flag !=null &&flag ==1){
			businessFile.setBusinessFile(UploadAndDownUtil.uploadImage(multipartFile, fileCategory));
		}
		businessFile.setOriginName(multipartFile.getOriginalFilename());
		businessFile.setCategory(fileCategory);
		businessFile.setOriginName(multipartFile.getOriginalFilename());
		businessFileDao.insert(businessFile);
		
	}

	@Override
	public int removeById(Long id) {
		return businessFileDao.deleteByPrimaryKey(id);
	}

	@Override
	public BusinessFile selectByKey(Long id) {
		return businessFileDao.selectByPrimaryKey(id);
	}

	@Override
	public void update(BusinessFile businessFile) {
		businessFileDao.updateByPrimaryKey(businessFile);
	}

	@Override
	public int removeByBusinesId(Long businessId, String category) {
		List<BusinessFile> list=businessFileDao.selectByBusinessId(Long.valueOf(businessId),category);
		int count=businessFileDao.deleteByBusinessId(Long.valueOf(businessId),category);
		for(BusinessFile file:list){
			UploadAndDownUtil.removeFile(file.getBusinessFile());
		}
		return count;
	}

	@Override
	public List<BusinessFile> selectListByBusinessId(Long businessId, String category) {
		return businessFileDao.selectByBusinessId(businessId, category);
	}

	@Override
	public void saveCustomer(String attachIds, List<MultipartFile> attachs, int businessId,String fileCategory) throws UfdmException {
		JSONArray arr=JSONArray.fromObject(attachIds);
		int i=0;
		for(;i<arr.size();i++){
			Long attachId=new Long(arr.get(i).toString());
			MultipartFile mFile=attachs.get(i);
			String path=UploadAndDownUtil.uploadFile(mFile, fileCategory);
			if(path.trim().isEmpty()){
				continue;
			}
			//保存数据到附件-客户对应表
			BusinessFile file=businessFileDao.selectByPrimaryKey(attachId);
			UploadAndDownUtil.removeFile(file.getBusinessFile());
			file.setBusinessFile(path);
			file.setOriginName(mFile.getOriginalFilename());
			businessFileDao.updateByPrimaryKeySelective(file);
		}
		for(;i<attachs.size();i++){
			MultipartFile mFile=attachs.get(i);
			String path=UploadAndDownUtil.uploadFile(mFile, fileCategory);
			if(path.trim().isEmpty()){
				continue;
			}
			//保存数据到附件-客户对应表
			BusinessFile file=new BusinessFile();
			file.setBusinessId(Long.valueOf(businessId));
			file.setBusinessFile(path);
			file.setOriginName(mFile.getOriginalFilename());
			file.setCreatedAt(new Date());
			file.setCategory(fileCategory);
			businessFileDao.insert(file);
		}
	}
	
	@Override
	public void removeCustomerFile(String delAttachIds) {
		if(delAttachIds==null||delAttachIds.trim().isEmpty()){
			return;
		}
		JSONArray arr=JSONArray.fromObject(delAttachIds);
		for(int i=0;i<arr.size();i++){
			long id=arr.getLong(i);
			BusinessFile file=businessFileDao.selectByPrimaryKey(id);
			businessFileDao.deleteByPrimaryKey(id);
			UploadAndDownUtil.removeFile(file.getBusinessFile());
		}
	}

	@Override
	public void saveImage(List<MultipartFile> images, Integer id, String category, int index, String image) throws UfdmException {
		if(images!=null&&images.size()!=0){
			for(int i=0;i<images.size();i++){
				MultipartFile mFile=images.get(i);
				BusinessFile businessFile=new BusinessFile();
				if(i==index){
					saveFile(id, category, image, mFile, businessFile);
					continue;
				}
				String path=UploadAndDownUtil.uploadImage(mFile, category);
				if(path.trim().isEmpty()){
					continue;
				}
				saveFile(id, category, path, mFile, businessFile);
			}
		}
	}
	
	

	private void saveFile(Integer id, String category, String image, MultipartFile mFile, BusinessFile businessFile) {
		businessFile.setBusinessId(id.longValue());
		businessFile.setCreatedAt(new Date());
		businessFile.setBusinessFile(image);
		businessFile.setOriginName(mFile.getOriginalFilename());
		businessFile.setCategory(category);
		businessFileDao.insert(businessFile);
	}

	@Override
	public List<String> getImage(Long businessId, String category) {
		return businessFileDao.selectImage(businessId, category);
	}

	@Override
	public void copyImage(List<String> images, Long id, String category, int mainImage, String image)
			throws UfdmException {
		for(int i=0;i<images.size();i++){
			BusinessFile file=new BusinessFile();
			if(i!=mainImage){
				String img=UploadAndDownUtil.copyImage(images.get(i), category);
				file.setBusinessFile(img);
			}else{
				file.setBusinessFile(image);
			}
			file.setCategory(category);
			file.setCreatedAt(new Date());
			file.setBusinessId(id);
			businessFileDao.insert(file);
		}
	}
}
